package com.easyOrder.core.mq;

import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import org.apache.log4j.Logger;

/** 
 * @description: 消息队列消费者 负责接收和处理消息
 */  
public class Reciever implements MessageListener{

	private Callback callback;

	
	private static final Logger logger = Logger.getLogger(Reciever.class);
	
	public Reciever(Callback callback){
		this.callback = callback;
	}
	
	public Action consume(Message message, ConsumeContext context) {
        try {
			//处理消息
			logger.debug("Pre consume: " + message.getTag() + " " + new String(message.getBody()));
			callback.execute(message);
			return Action.CommitMessage;
		}catch (Exception e) {
            //消费失败
        	logger.error(e, e);
            return Action.ReconsumeLater;
        }
    }

}
